package com.izhaowo.recommend.contrl;

import com.alibaba.fastjson.JSON;
import com.izhaowo.recommend.vo.PlannerQueryInfoVO;
import com.izhaowo.recommend.vo.PlannerRecomRankVO;
import com.izhaowo.recommend.vo.WorkerQueryInfoVO;
import com.izhaowo.recommend.vo.WorkerVO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
 * Created by HASEE on 2019/7/22.
 */
public class RecommandWorker {
    static RecommendWeddingPlanner hander = new RecommendWeddingPlanner();

    public static List<PlannerQueryInfoVO> processRecomWorker(
            List<PlannerRecomRankVO> plannerList, String workerId, String weddate, String typeStr) {
        List<PlannerQueryInfoVO> resultList = new ArrayList<>();
//        if (nonNull(workerId)) {
        if (null != workerId && !"".endsWith(workerId)) {
            WorkerVO worker = hander.getWorkerVOById(workerId);   //izhaowo_worker.tb_worker tb_vocation tb_worker_location tb_worker_service
//            if (null != worker && worker.getVocation().equals("策划师")) {
            if (null != worker && worker.getVocationId().equals("d677824c-bae7-11e7-99f0-408d5c0cd580")) {
                //查询该策划师在本场婚礼是否可接单
//                List<WorkerQueryInfoVO> recomWorkersList = CombineAction.getFuzzyWorkers(worker.getName(), weddate);
                List<WorkerQueryInfoVO> recomWorkersList = new ArrayList<>();
                if (null != weddate && !"".equals(weddate)) {
                    recomWorkersList = CombineAction.getFuzzyWorkers(worker.getName(), weddate ,typeStr);
                } else {
                    recomWorkersList = CombineAction.getFuzzyWorkersForCrm(worker.getName(), -1, -1);
                }
                if (nonNull(recomWorkersList) && !recomWorkersList.isEmpty()) {
                    for (WorkerQueryInfoVO vo : recomWorkersList) {
                        if (vo.getId().equals(workerId)) {
                            //基本档期，限单等信息合并
                            PlannerQueryInfoVO info = new PlannerQueryInfoVO();
                            BeanUtils.copyProperties(vo, info);
                            //判断是否有推荐排名信息
                            // 获取策划师排名标签

                            if (!isNull(plannerList) && plannerList.size() > 0 && JSON.toJSONString(plannerList).contains(workerId)) {
                                Map<String, List<PlannerRecomRankVO>> recomRankMap = plannerList.stream().collect(Collectors.groupingBy(PlannerRecomRankVO::getWorkerId));
                                List<PlannerRecomRankVO> plannerRecomList = recomRankMap.get(worker.getId());
                                if (!isNull(plannerRecomList) && plannerRecomList.size() > 0) {
                                    PlannerRecomRankVO recom = plannerRecomList.get(0);

                                    //直接将该策划师数据展示，不进行区间段&标签筛选
                                    List<String> planneTagList = new ArrayList<>();
                                    if ("v2".equals(recom.getVersion())) {
                                        planneTagList = recom.getTags();
                                    } else {
                                        for (PlannerRecomRankVO vo1 : plannerRecomList) {
                                            planneTagList.add(vo1.getType());
                                        }
                                    }


                                    // 推荐信息合并
                                    info.setWorkerId(recom.getWorkerId());
                                    info.setCaseRate(recom.getCaseRate());
                                    info.setReorderRate(recom.getReorderRate());
                                    info.setPreSpeScore(recom.getPreSpeScore());
                                    info.setAllScoreFinal(recom.getAllScoreFinal());
                                    info.setSot(recom.getSot());
                                    info.setScoreFlag1(recom.getScoreFlag1());
                                    info.setScoreFlag2(recom.getScoreFlag2());
                                    info.setScoreFlag3(recom.getScoreFlag3());
                                    info.setScoreFlag4(recom.getScoreFlag4());
                                    info.setScoreFlag5(recom.getScoreFlag5());
                                    info.setTagList(planneTagList);
                                    info.setProvinceName(recom.getProvince());
                                    info.setCityName(worker.getCityName());
                                    info.setServiceCity(null);
                                    info.setProfile(null);
                                    info.setHomePage(null);
                                }
                            }
                            info.setWorkerId(workerId);
                            info.setType(1);
                            info.setHotelScore(-99);
                            resultList.add(info);
                            break;
                        }
                    }
                }
            }
        }
        return resultList;
    }
}
